/*
 * Copyright (c) 2021 Talkweb Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "tw_nfc_kits.h"

static void nfc_write_tag(void)
{
    TWNfcI2cInit();
    osDelay(10);

    NdefFrame param[3] = // 初始化三个标签的参数
        {
            {
                .packtype = START_PACK,                                        // 包类型， 为开始包
                .dataType = RTD_UTF8,                                          // 内容格式， 为 UTF8格式
                .payloadLen = strlen("hello, this is nfc write tag example!"), // 内容长度
                .payload = "hello, this is nfc write tag example!",            // 具体内容

            },
            {
                .packtype = MIDDLE_PACK,             // 包类型， 为中间包
                .dataType = RTD_URI,                 // 内容格式， 为 URI格式
                .uriVal = RTD_URI_httpsWWW,          // URI具体格式， 为URI格式中的https
                .payloadLen = strlen("talkweb.com"), // 内容长度
                .payload = "talkweb.com",            // 具体内容

            },
            {
                .packtype = END_PACK,                   // 包类型， 为结束包
                .dataType = RTD_UTF8,                   // 内容格式， 为 UTF8格式
                .payloadLen = strlen("openHarmony OS"), // 内容长度
                .payload = "openHarmony OS",            // 具体内容
            }};

    if (TWNfcTagWrite(param, 3) != TRUE) // 写入三个标签到NFC
    {
        printf("TWNfcTagWrite() err!\r\n");
    }
    else
    {
        printf("TWNfcTagWrite() OK!\r\n");
    }

    for (;;)
    {
        osDelay(100);
    }
}

static void TW_nfc_example(void)
{
    osThreadAttr_t attr;

    attr.name = "nfc_write_tag";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 8;
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t)nfc_write_tag, NULL, &attr) == NULL)
    {
        printf("Falied to create nfc_read_tag!\n");
    }
}

APP_FEATURE_INIT(TW_nfc_example);
